var interval_page2 ;
//false时初始化所有数据，true 更新数据
var flag_page2 =false;
$(function(){
    getspotPrice_page2();
    interval_page2=setInterval(function() {
        getspotPrice_page2();
    }, 1000);

    setInterval(function() {
        $(".page2_tdprice").css("color","black");//移除刷新
    }, 8000);

    //定时移除所有自选，重新加载
    setInterval(function() {
        flag_page2 = false;
    }, 3000);
});

function getspotPrice_page2(){
    $.ajax({
        url: "selectPrice",
        type: 'post',
        dataType:'json',
        async: false,
        success: function (msg) {
            if (flag_page2 == false) {
                $("#page2Tbody").find("tr").remove();//删除所有tr
            }
            for(var i=0;i<msg.length;i++){
                createTr_page2(msg[i],i);
            }
            flag_page2=true;
        },
        error:function(XMLHttpRequest, textStatus, errorThrown) {
            clearInterval(interval_page2 );//停止
        }});
};

function removeSelect(btn){
    var $tds = $(btn).parent().siblings();
    var td1 = $tds.eq(0).html();
    // alert("添加自选成功");
    $.ajax({
        url: "removeSelect",
        type: "post",
        async: false,
        data: {"indexName": td1},
        success: function (respResult) {
            // alert("移除自选成功");
            var tr_class ='str_page2'+td1;
            $("."+tr_class).remove();//移除
        },
        error: function (respResult) {
            Alert('失败');
        }
    });
}


function createTr_page2(data,i) {
    var indexName = data.indexName;
    var ask = data.ask;
    var bid = data.bid;
    var strLine = '<tr class='+"str_page2"+indexName+'>';
    if (i % 2 != 0) {
        strLine = '<tr style="background:#0000000A" class='+"str_page2"+indexName+'>';
    }
    var $tr =
        // $("<tr class='str_page2"+indexName+"'>"+
        $(strLine+
            '<td style="display:none">'+indexName+'</td>'+
            '<td><a href="" data-rel="external">'+indexName+'</a></td>'+
            '<td align="center" class ="page2_tdprice" id='+indexName+'_ask_page2'+'>'+ask+'</td>'+
            '<td align="center" class ="page2_tdprice"  id='+indexName+'_bid_page2'+'>'+bid+'</td>'+
            // '<td>'+'自选'+'</td>'+
            '<td align="center"><input type="button" style="background:#FAEBD7" onclick="removeSelect(this);" value="-"/></td>'+
            '</tr>');
    //追加到tbody下
    if (flag_page2 == false) {
        $("#page2Tbody").append($tr);
    }else{
        var askid=indexName+'_ask_page2';
        var bidid=indexName+'_bid_page2';
        var askIdValue=$("#"+askid).text()
        var bidIdValue=$("#"+bidid).text()
        $("#"+askid).text(ask);
        $("#"+bidid).text(bid);
        if (askIdValue < ask) {
            //indexName+'_ask_page2' 渲染颜色
            $("#"+askid).css("color","red");
            $("#"+bidid).css("color","red");
        }else if (askIdValue > ask) {
            $("#"+askid).css("color","green");
            $("#"+bidid).css("color","green");
        }
        // $('"'+class1+'"').remove();//移除刷新
        // $("#goods").append($tr);
    }



}